<?php
namespace api\know\controller;

use api\know\model\UserLastRecordModel;
use api\know\model\GrammarModel;
use api\know\model\NewWordModel;
use api\know\model\SentenceModel;
use api\know\model\TeachingMaterialContentModel;
use api\know\model\PaperModel;
use api\know\controller\KnowRestUserController;

class UserLastRecordController extends KnowRestUserController
{
    public function lastest()
    {
        $uid = $this->getUserId('uid');
        // $uid = $this->request->param('uid', 0);
        
        $w = ['uid' => $uid];
        $rows = UserLastRecordModel::where($w)->select();

        // 教材  TeachingMaterial
        // 资料  LearningMaterial
        // 词汇 NewWord
        // 语法 Grammar
        // 佳句 Sentence
        // 佳作
        // 章节练习 ZJLX
        $d = [];
        foreach($rows as $v)
        {
            if ($v['type'] === 'TeachingMaterial')
            {
                $wtm['a.is_delete'] = 0;
                $wtm['a.chapter_uuid'] = $v['relate_uuid'];

                $field = array_map(function($s){
                    return 'a.' . $s;
                }, TeachingMaterialContentModel::$QueryField);
                array_push($field, 'b.name as chapter_name');
                $tm = TeachingMaterialContentModel::where($wtm)
                            ->alias('a')
                             ->leftJoin('cmf_know_chapter b', 'a.chapter_uuid = b.uuid and a.material_uuid = b.relate_uuid')
                            ->field($field)
                            ->find();

                if (empty($tm))
                {
                    continue;
                }

                $tm['content'] = $this->decodeContent($tm['content']);
                $tm['type'] = $v['type'];
                $tm['typeName'] = '教材';

                array_push($d, $tm);
            }
            else if ($v['type'] === 'LearningMaterial') 
            {

            }
            else if ($v['type'] === 'NewWord') 
            {
                $wnw['is_delete'] = 0;
                $wnw['uuid'] = $v['relate_uuid'];

                $nw = NewWordModel::where($wnw)
                            ->field(NewWordModel::$QueryField)
                            ->find();
                if (empty($nw))
                {
                    continue;
                }
                $nw['type'] = $v['type'];
                $nw['typeName'] = '词汇';

                array_push($d, $nw);
            }
            else if ($v['type'] === 'Grammar') 
            {
                $wng['is_delete'] = 0;
                $wng['uuid'] = $v['relate_uuid'];

                $gm = GrammarModel::where($wng)
                            ->field(GrammarModel::$QueryField)
                            ->find();
                if (empty($gm))
                {
                    continue;
                }
                $gm['type'] = $v['type'];
                $gm['typeName'] = '语法';
                array_push($d, $gm);
            }
            else if ($v['type'] === 'Sentence') 
            {
                $wsm['is_delete'] = 0;
                $wsm['uuid'] = $v['relate_uuid'];

                $st = SentenceModel::where($wsm)
                            ->field(SentenceModel::$QueryField)
                            ->find();
                if (empty($st))
                {
                    continue;
                }

                $st['type'] = $v['type'];
                $st['typeName'] = '佳句';
                array_push($d, $st);
            }
            else if ($v['type'] === 'ZJLX') 
            {
                $wsm['is_delete'] = 0;
                $wsm['uuid'] = $v['relate_uuid'];

                $p = PaperModel::where($wsm)
                            ->field(PaperModel::$QueryField)
                            ->find();
                if (empty($p))
                {
                    continue;
                }

                $p['type'] = $v['type'];
                $p['typeName'] = '章节练习';
                $p['chapter_name'] = $p['name'];
                $p['content'] = $p['name'];
                array_push($d, $p);
            }
        }

        $this->success('请求成功!', $d);
    }

    public function add()
    {
        $uid = $this->getUserId('uid');
        $type = $this->request->param('type');
        $relate_uuid = $this->request->param('relate_uuid');
        
        $w = ['uid' => $uid, 'type' => $type];
        $row = UserLastRecordModel::where($w)->find();
        if ($row)
        {
            UserLastRecordModel::where('id', $row['id'])->save([
                'relate_uuid' => $relate_uuid
            ]);
        }
        else
        {
            $r = [
				'uid' => $uid,
				'type' => $type,
				'relate_uuid' => $relate_uuid,
			];
			UserLastRecordModel::create($r);
        }
        $this->success('请求成功!');
    }



}
